package com.hoperun.controller.common;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.hoperun.bean.AdminManager;
import com.hoperun.framework.ex.XHZErrorCode;
import com.hoperun.framework.ex.XHZException;
import com.hoperun.framework.message.NLS;
import com.hoperun.framework.models.DataView;
import com.hoperun.service.IAdminManagerSV;
import com.hoperun.utils.Constant;
import com.hoperun.utils.ErrorCode;
import com.hoperun.utils.LoginHelper;
import com.hoperun.utils.SuccessCode;
import com.hoperun.utils.XHZUtils;

/**
 * Copyright: Copyright (c) 2014 CNPCMALL <br/>
 * ClassName: LoginoutController.java
 * 
 * @Description: 该类的功能描述
 * 
 * @version: v1.0.0
 * @author: fuyu
 * @date: 2014-7-22 下午2:10:32
 * 
 *        Modification History: Date Author Version Description
 *        ---------------------------------------------------------* 2014-7-22
 *        fuyu v1.0.0 修改原因
 */
@Controller
public class LoginoutController extends BaseController {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(LoginoutController.class);

	@Resource
	private IAdminManagerSV adminManagerSV;

	@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
	public String doLogin(String username, String password, boolean rememberMe) {
		try {
			LoginHelper.login(username, password, rememberMe);
		} catch (AuthenticationException ae) {
			// do noting
		}
		return "redirect:/";
	}

	@RequestMapping(value = "/doAjaxLogin", method = RequestMethod.POST)
	@ResponseBody
	public DataView doAjaxLogin(String username, String password,
			boolean rememberMe, final HttpServletRequest request) {

		DataView dataView = new DataView();

		if ((null == username || "".equals(username.trim()))
				&& (null == password || "".equals(password.trim()))) {
			dataView.setCode(0);
			dataView.setMessage("请输入用户名或密码");
			return dataView;
		} else if (null == username || "".equals(username.trim())) {
			dataView.setCode(0);
			dataView.setMessage("用户名不能为空");
			return dataView;
		} else if (null == password || "".equals(password.trim())) {
			dataView.setCode(0);
			dataView.setMessage("密码不能为空");
			return dataView;
		}
		try {
			LoginHelper.login(username, password, rememberMe);

			dataView.setCode(1);
			dataView.setMessage("success");
		} catch (AuthenticationException ae) {
			dataView.setCode(0);
			dataView.setMessage("用户名或密码错误");
		}
		return dataView;
	}

	@RequestMapping(value = "/login")
	public String login(final HttpServletRequest request) {
		return "alone:admin.login";
	}

	@RequestMapping(value = "/change/password", method = RequestMethod.GET)
	public String changePassword(final NativeWebRequest request) {

		return "alone:admin.password";
	}

	@RequestMapping(value = "/change/password", method = RequestMethod.POST)
	@ResponseBody
	public DataView password(String password, String newPassword,
			final HttpServletRequest request) {
		DataView dataView = new DataView();
		if (StringUtils.isBlank(password) || StringUtils.isBlank(newPassword)) {
			throw new XHZException(XHZErrorCode.MISS_PARAM, null,
					new Object[] { "密码" });
		}

		AdminManager adminManager = LoginHelper.getLoginUser();
		if (!XHZUtils.generatePassword(password).equals(
				adminManager.getPassword())) {
			throw new XHZException(ErrorCode.MEMBER.ACCOUNT_PASSWORD_ILLEGAL);
		}

		adminManager.setPassword(XHZUtils.generatePassword(newPassword));
		try {
			adminManagerSV.update(adminManager);
			dataView.setCode(Constant.SUCCESS);
			dataView.setMessage(NLS
					.getMessage(SuccessCode.USER_CHANGE_PASSWORD));
			LoginHelper.logout();

		} catch (Exception e) {
			LOGGER.debug(e.getMessage());
		}
		return dataView;
	}

	@RequestMapping("/logout")
	public String logout() {
		LoginHelper.logout();
		return "redirect:/";
	}

	@RequestMapping("/exit")
	@ResponseBody
	public DataView exit(final HttpServletRequest request) {
		DataView dataView = new DataView();
		try {
			LoginHelper.logout();

		} catch (Exception e) {
			return dataView;
		}

		return dataView;
	}
}
